﻿//-----------------------------------------------------------------------
// <copyright file="FCISPlusClientCommunicator.cs" company="FCIS">
//     Company copyright tag.
// </copyright>
// <author>Ramy Medhat</author>
// <date>24/8/2011</date>
// <reviewer></reviewer>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FCISPlus.Client.Communicator
{
    /// <summary>
    /// Client Communicator for In-memory transfers
    /// </summary>
    internal class FCISPlusClientCommunicator : IFCISPlusClientCommunicator
    {
        /// <summary>
        /// The private token for the current client session.
        /// </summary>
        private int token = -1;

        /// <summary>
        /// The FCIS+ server.
        /// </summary>
        private FCISPlus.Server.Communicator.IFCISPlusServerCommunicator server;

        /// <summary>
        /// Initializes a new instance of the <see cref="FCISPlusClientCommunicator"/> class.
        /// </summary>
        public FCISPlusClientCommunicator()
        {
            this.server = FCISPlus.Server.FCISPlusServerFactory.GetCommunicator();
        }

        #region IFCISPlusClientCommunicator Members

        /// <summary>
        /// Gets all users.
        /// </summary>
        /// <returns>Array of all users.</returns>
        public FCISPlus.Server.Database.User[] GetAllUsers()
        {
            return server.GetAllUsers();
        }

        /// <summary>
        /// Logins the specified username.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns>
        /// Boolean indicating the success of the login.
        /// </returns>
        public bool Login(string username, string password)
        {
            this.token = this.server.Login(username, password);

            if (this.token != -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Signups the specified username.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns>
        /// Boolean indicating the success of the singup.
        /// </returns>
        public bool Signup(string username, string password)
        {
            this.token = this.server.SignUp(username, password);

            if (this.token != -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Logouts the specified username.
        /// </summary>
        public void Logout()
        {
            this.server.Logout(this.token);
        }

        /// <summary>
        /// Gets the user profile.
        /// </summary>
        /// <returns>
        /// The user object.
        /// </returns>
        public Server.Database.User GetUserProfile()
        {
            return this.server.GetUser(this.token);
        }

        /// <summary>
        /// Gets the feed.
        /// </summary>
        /// <returns>
        /// Array of posts from the user or his/her friends
        /// </returns>
        public Server.Database.Post[] GetFeed()
        {
            return this.server.GetFeeds(this.token);
        }

        /// <summary>
        /// Adds the post.
        /// </summary>
        /// <param name="postText">The post text.</param>
        /// <param name="postLink">The post link.</param>
        public void AddPost(string postText, string postLink)
        {
            this.server.AddPost(postText, postLink, token);
        }

        /// <summary>
        /// Sends the message.
        /// </summary>
        /// <param name="friend">The friend.</param>
        /// <param name="messageText">The message text.</param>
        public void SendMessage(Server.Database.User friend, string messageText)
        {
            this.server.SendMessage(friend, messageText, token);
        }

        /// <summary>
        /// Gets the recent conversations.
        /// </summary>
        /// <param name="messageLifeTime">The message life time.</param>
        /// <returns></returns>
        public Server.Database.Message[] GetRecentConversations(TimeSpan messageLifeTime)
        {
            return this.server.GetRecentConversations(messageLifeTime, token);
        }

        /// <summary>
        /// Gets the conversation by friend.
        /// </summary>
        /// <param name="friend">The friend.</param>
        /// <param name="messageLifeTime">The message life time.</param>
        /// <returns></returns>
        public FCISPlus.Server.Database.Message[] GetConversationByFriend(FCISPlus.Server.Database.User friend, TimeSpan messageLifeTime)
        {
            return this.server.GetConversationByFriend(friend, messageLifeTime, token);
        }
        /// <summary>
        /// Gets the friends.
        /// </summary>
        /// <returns></returns>
        public FCISPlus.Server.Database.User[] GetFriends()
        {
            return this.server.GetFriends(token);
        }

        /// <summary>
        /// Adds a new friend.
        /// </summary>
        /// <returns></returns>
        public void AddFriend(FCISPlus.Server.Database.User user)
        {
            this.server.AddFriend(user, token);
        }

        /// <summary>
        /// Removes a friend.
        /// </summary>
        /// <returns></returns>
        public void RemoveFriend(FCISPlus.Server.Database.User user)
        {
            this.server.RemoveFriend(user, token);
        }

        /// <summary>
        /// Likes the specified post.
        /// </summary>
        /// <param name="post">The post.</param>
        public void Like(FCISPlus.Server.Database.Post post)
        {
            this.server.Like(post, token);
        }

        /// <summary>
        /// Unlikes the specified post.
        /// </summary>
        /// <param name="post">The post.</param>
        public void Unlike(FCISPlus.Server.Database.Post post)
        {
            this.server.Unlike(post, token);
        }

        #endregion
    }
}
